package com.tcoiss.datafactory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcoiss.common.core.exception.CustomException;
import com.tcoiss.common.datasource.DBContextHolder;
import com.tcoiss.common.datasource.DataSourceVo;
import com.tcoiss.common.datasource.DynamicDataSource;
import com.tcoiss.datafactory.domain.DataSource;
import com.tcoiss.datafactory.mapper.DataSourceMapper;
import com.tcoiss.datafactory.service.IDataSourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 数据源配置Service业务层处理
 *
 * @author zw
 * @date 2021-01-19
 */
@Service
public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSource> implements IDataSourceService {

    @Autowired
    DynamicDataSource dynamicDataSource;

    @Override
    public List<DataSource> queryList(DataSource dataSource) {
        LambdaQueryWrapper<DataSource> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dataSource.getIpAddress())){
            lqw.eq(DataSource::getIpAddress ,dataSource.getIpAddress());
        }
        if (StringUtils.isNotBlank(dataSource.getDatabaseType())){
            lqw.eq(DataSource::getDatabaseType ,dataSource.getDatabaseType());
        }
        if (dataSource.getCreateDate() != null){
            lqw.eq(DataSource::getCreateDate ,dataSource.getCreateDate());
        }
        if (StringUtils.isNotBlank(dataSource.getDatabaseName())){
            lqw.like(DataSource::getDatabaseName ,dataSource.getDatabaseName());
        }
        return this.list(lqw);
    }

    @Override
    public void checkDB(DataSource dataSource) {
        try {
            DataSourceVo vo = new DataSourceVo();
            vo.setUserName(dataSource.getUserName());
            vo.setPassWord(dataSource.getPassWord());
            vo.setIpPort(dataSource.getIpPort());
            vo.setIpAddress(dataSource.getIpAddress());
            vo.setConncetAttr(dataSource.getConncetAttr());
            vo.setDbName(dataSource.getDbName());
            vo.setDatabaseType(dataSource.getDatabaseType());
            vo.setDatabaseName(dataSource.getDatabaseName());
            dynamicDataSource.createDataSourceWithCheck(vo);
        }catch (Exception e){
            throw new CustomException("检查数据连接异常",e);
        }

    }

    @Override
    public boolean changeDbSource(Long id) {
        DataSource dataSource = this.getById(id);
        try {
            DataSourceVo vo = new DataSourceVo();
            vo.setUserName(dataSource.getUserName());
            vo.setPassWord(dataSource.getPassWord());
            vo.setIpPort(dataSource.getIpPort());
            vo.setIpAddress(dataSource.getIpAddress());
            vo.setConncetAttr(dataSource.getConncetAttr());
            vo.setDbName(dataSource.getDbName());
            vo.setDatabaseType(dataSource.getDatabaseType());
            vo.setDatabaseName(dataSource.getDatabaseName());
            dynamicDataSource.createDataSourceWithCheck(vo);
            DBContextHolder.setDataSource(dataSource.getDatabaseName());
        } catch (Exception e) {
            throw new CustomException("切换数据源异常",e);
        }
        return true;
    }

    @Override
    public boolean updateUseStatus(List<String> asList, Integer status) {
        if (asList != null & status != null) {
            for (String id : asList) {
                DataSource ew = getById(id);
                if (ew.getDataLevel() != status) {
                    ew.setDataLevel(status);
                    this.updateById(ew);
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public DataSource getDataSourceByDbName(String dbName) {
        LambdaQueryWrapper<DataSource> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dbName)){
            lqw.eq(DataSource::getDbName ,dbName);
        }

        return this.getOne(lqw);
    }


}
